//
//  HeaderGoldPriceView.m
//  PriceOnline
//
//  Created by Le Anh Tai on 7/23/13.
//  Copyright (c) 2013 Le Anh Tai. All rights reserved.
//

#import "HeaderGoldPriceView.h"
#import "UIConstants.h"

@implementation HeaderGoldPriceView

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {        
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.font = FONT_HELVETICANEUE(13);
        [self addSubview:_titleLabel];
        
        _lineView = [[LineView alloc]init];
        [self addSubview:_lineView];
        
        [self reloadTheme];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:NOTIFICATION_CHANGE_THEME object:nil];
    }
    
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if( point.x > 0 && point.x < self.frame.size.width && point.y > 0 && point.y < self.frame.size.height )
    {
        if ([_mainDelegate respondsToSelector:@selector(headerGoldPriceViewDidTap:atPoint:)])
        {
            [_mainDelegate headerGoldPriceViewDidTap:self atPoint:point];
        }
    }
    
    return [super hitTest:point withEvent:event];
}

- (void)reloadTheme
{
    if ([DataSingleton shared].isDarkTheme)
    {
        self.backgroundColor = COLOR_BACKGROUND_HEADER_DARK;
        _titleLabel.textColor = COLOR_TEXT_HEADER_DARK;
    }
    else
    {
        self.backgroundColor = COLOR_BACKGROUND_LIGHT;
        _titleLabel.textColor = COLOR_TEXT_HEADER_LIGHT;
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    _lineView.frame = RECT(0, 0, self.width, 2);
    
    _titleLabel.frame = RECT(10, 0, self.width - 10, self.height);    
}

- (void)handleNotification:(NSNotification *)sender
{
    [self reloadTheme];
}

@end
